/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webtransac.comercial.controller.util;

/**
 *
 * @author efranco
 */
import java.util.LinkedList;
import java.util.List;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.DefaultTreeModel;
import org.zkoss.zul.DefaultTreeNode;
import org.zkoss.zul.Grid;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.TreeModel;
import org.zkoss.zul.TreeNode;
//import demo.getting_started.Car;
//import demo.getting_started.CarService;
//import demo.getting_started.CarServiceImpl;
import com.webtransac.comercial.entity.Menu;
 
public class CategoryController extends SelectorComposer<Component> {
    private static final long serialVersionUID = 1L;
 
    //private CarService CarService = new CarServiceImpl();
    private Menu menu = new Menu();
    private TreeModel<TreeNode<Menu>> categoriesModel;
 
    //@Wire
    //private Grid resultGrid;
 
    public CategoryController() {
        // construct tree nodes by categories
        //Category categoryRoot = carService.getCarCategoriesRoot();
        Menu menuRoot = new Menu();         
        Menu subMenu1 = new Menu();       
        Menu subMenu2 = new Menu();               
        List<Menu> children = new LinkedList<Menu>();        
                
        menuRoot.setIdMenu("01");
        menuRoot.setDescripcion("Menu general");

        subMenu1.setIdMenu("S1");
        subMenu1.setDescripcion("Importaciones");
        
        subMenu2.setIdMenu("S2");
        subMenu2.setDescripcion("Compras Nacionales");
                
        children.add(subMenu1);
        children.add(subMenu2);
                
        menuRoot.setChildren(children);
                
                
        // CategoryTreeNode rootNode = constructCategoryTreeNode(categoryRoot);
        CategoryTreeNode rootNode = constructCategoryTreeNode(menuRoot);
        
        // create tree model
        categoriesModel = new DefaultTreeModel<Menu>(rootNode);
        ((DefaultTreeModel<Menu>)categoriesModel).addOpenPath(new int[]{0});
    }
 
    private CategoryTreeNode constructCategoryTreeNode(Menu menu) {
        //CategoryTreeNode categoryNode = new CategoryTreeNode(menu, carService.countByFilter(category.getName()));
        CategoryTreeNode categoryNode = new CategoryTreeNode(menu, 0);
        LinkedList<CategoryTreeNode> queue = new LinkedList<CategoryTreeNode>(); // BFS
        queue.add(categoryNode);
        while(!queue.isEmpty()) {
            CategoryTreeNode node = queue.remove();
            for(Menu childCategory : node.getData().getChildren()) {
               // CategoryTreeNode child = new CategoryTreeNode(childCategory,carService.countByFilter(childCategory.getName()));
                CategoryTreeNode child = new CategoryTreeNode(childCategory,0);
                node.add(child);
                queue.add(child);
            }
        }
        CategoryTreeNode rootNode = new CategoryTreeNode(null,-1); // won't show
        rootNode.add(categoryNode);
        return rootNode;
    }
 
    public TreeModel<TreeNode<Menu>> getCategoriesModel() {
        return categoriesModel;
    }
 
   /* @Listen("onSelect = #categoriesTree")
    public void displayCars() {
        TreeNode<Category> selectedNode = ((DefaultTreeModel<Category>)categoriesModel).getSelection()
                .iterator().next();
        Category selectedCategory = selectedNode.getData();
        List<Car> cars = carService.queryByFilter(selectedCategory.getName());
        resultGrid.setModel(new ListModelList<Car>(cars)); 
    }*/
}